//
//  SHWelcomeViewController.swift
//  SHWeibo3
//
//  Created by teacher on 16/7/22.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit

class SHWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.headImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        })
        
        // 开始动画
        ///
        ///
        /// - parameter <Tduration:             动画时间
        /// - parameter delay:                  演示时间
        /// - parameter usingSpringWithDamping: 阻尼系数（0-1） 阻尼数约大 代表弹性效果越差
        /// - parameter initialSpringVelocity:  加速度
        /// - parameter options:
        /// - parameter animations:
        /// - parameter completion:
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 对当前页面进行刷新
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 当上个动画完成后
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        // 发送通知 切换根控制器为MainVc
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "welcome")
                })
        }
    }

    // MARK: - 设置视图
    private func setupUI(){
        // 添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }

    }
    
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(imgName: "ad_background")
    // 头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(imgName: "avatar_default_big")
        img.sh_setImage(SHUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeholderImgName: "avatar_default_big")
        img.layer.borderColor = ThemeColor.CGColor
        img.layer.borderWidth = 2
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        return img
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGrayColor(), textFont: 18)
        lab.text = "欢迎回来"
        lab.textAlignment = .Center
        lab.alpha = 0
        return lab
    }()

}
